<template>
  <div class="selector-input">
    <label class="placeholder"> {{ placeholder }} </label>
    <input
      type="text"
      class="input"
      :value="value"
      ref="inputValue"
      @input="searchOptions($event)"
      @focus="searchOptions($event)"
      @blur="setValue(value)"
    />
    <span class="iconfont icon-arrow-down"></span>
  </div>
</template>

<script>
import { getCurrentInstance } from 'vue';

export default {
  name: 'SelectorInput',
  props: {
    placeholder: {
      type: String,
      default: '请选择'
    },
    value: String
  },
  setup(props, { emit }) {
    const instance = getCurrentInstance()

    const searchOptions = (e) => {
      // console.log(e)
      emit('searchOptions', e.target.value);
    }

    const setValue = (value) => { // 利用ref 实现输入框记忆功能
      const _input = instance.refs.inputValue;

      if (_input.value.length > 0) {
        _input.value = value;
      }
    }

    return {
      searchOptions,
      setValue
    }
  }
}
</script>

<style lang="scss" scoped>
  .selector-input {
    position: relative;
    width: 100%;
    height: 38px;

    .input {
      width: 100%;
      height: 100%;
      padding: 0 15px;
      background-color: #fff;
      border-radius: 5px;
      border: 1px solid #999;
      box-sizing: border-box;
      outline: none;
      transition: all .2s linear;

      &:focus {
        border-color: #32caeb;
        box-shadow: 0 0 3px #32caeb;
      }
    }

    .placeholder,
    .iconfont {
      position: absolute;
    }

    .placeholder {
      left: 15px;
      top: 8px;
      color: #999;
    }
    .iconfont {
      right: 15px;
      top: 12px;
      color: #999;

      &.icon-search {
        font-size: 22px;
        top: 8px;
      }
    }
  }
</style>